package com.acwing.lc.part1;

/**
 * @author rkc
 * @date 2022/9/15 15:40
 */
public class LC52N皇后II {

    private final int N = 10;
    private boolean[] col = new boolean[N], dg = new boolean[100], udg = new boolean[100];
    private char[][] g;
    private int n, ans;

    public int totalNQueens(int _n) {
        n = _n;
        g = new char[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                g[i][j] = '.';
            }
        }
        dfs(0);
        return ans;
    }

    private void dfs(int u) {
        if (u == n) {
            ans++;
            return;
        }
        for (int i = 0; i < n; i++) {
            if (!col[i] && !dg[u - i + n] && !udg[i + u]) {
                col[i] = dg[u - i + n] = udg[i + u] = true;
                g[u][i] = 'Q';
                dfs(u + 1);
                g[u][i] = '.';
                col[i] = dg[u - i + n] = udg[i + u] = false;
            }
        }
    }
}
